package com.mk.carcassonne;

import java.util.ArrayList;
import java.util.HashMap;

import com.mk.carcassonne.drawables.RotatableImageView;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class PlaceFollowerActivity extends Activity {

	private int DOT_RADIUS = 11;
	private int BRICK_IMAGE_SIZE = 80;
	
	private HashMap<Position, ImageView> _dots = new HashMap<Position, ImageView>();
	
	private FollowerPlacement _selectedFollowerPlacement;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.place_follower);
        
        RotatableImageView nextBrickImageView = (RotatableImageView)findViewById(R.id.brickImage);
        nextBrickImageView.setRotation(GlobalData.getLastBrick().getRotation() * Global.DEFAULT_ROTATION);
        Drawable brickImage = getResources().getDrawable(GlobalData.getLastBrick().getLargeBrickId());
        nextBrickImageView.setImageDrawable(brickImage);
        
        ArrayList<FollowerPlacement> followerPlacements = GlobalData.getLastBrick().getFollowerPlacementCollection().getPlacements();
        
        final ListView itemContainer = (ListView)findViewById(R.id.itemContainer);
        itemContainer.setAdapter(new ArrayAdapter<FollowerPlacement>(this, android.R.layout.simple_list_item_1, followerPlacements));
        itemContainer.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        
        RelativeLayout imageContainer = (RelativeLayout)findViewById(R.id.imageContainer);
        
        Integer leftEdge = Global.SCREEN_SIZE_X / 2 - BRICK_IMAGE_SIZE / 2;
        Integer topEdge = 20;
        for(FollowerPlacement followerPlacement : followerPlacements)
        {
        	if(followerPlacement.isAvailable())
        	{
	        	ImageView dotImageView = new ImageView(this);
	        	dotImageView.setImageResource(R.drawable.yellow_dot_small);
	        	Position rotatedFollowerPlacementPosition = followerPlacement.getRotatatedPosition(GlobalData.getLastBrick().getRotation());
	        	int left = leftEdge + (int)(rotatedFollowerPlacementPosition.getX() / 100.0 * BRICK_IMAGE_SIZE) - DOT_RADIUS;
	        	int right = topEdge + (int)(rotatedFollowerPlacementPosition.getY() / 100.0 * BRICK_IMAGE_SIZE) - DOT_RADIUS;
	        	
	        	dotImageView.setPadding(left, right, 0, 0);
	        	
	        	_dots.put(followerPlacement.getPosition(), dotImageView);
	        	
	        	imageContainer.addView(dotImageView);
        	}
        }
        
        itemContainer.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				_selectedFollowerPlacement = (FollowerPlacement)parent.getItemAtPosition(position);
				
				for (ImageView iv : _dots.values()) {
					iv.setImageResource(R.drawable.yellow_dot_small);
				}
				
				ImageView iv = _dots.get(_selectedFollowerPlacement.getPosition());
				iv.setImageResource(R.drawable.yellow_dot_small_glow);
				
				//for(int i = 0; i < itemContainer.getChildCount(); i++)
				//{
				//	itemContainer.getChildAt(i).setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
				//}
				//view.setBackgroundDrawable(getResources().getDrawable(R.color.lightbrown));
			}
		});
        
        Button cancelPlaceFollowerButton = (Button)findViewById(R.id.cancelPlaceFollowerButton);
        cancelPlaceFollowerButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
        
        Button placeFollowerButton = (Button)findViewById(R.id.placeFollowerButton);
        placeFollowerButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(_selectedFollowerPlacement != null)
				{
					GlobalData.placeFollower(GlobalData.getLastBrick(), _selectedFollowerPlacement, 1);
					Global.SoundManager.playSound(R.raw.shwoop);
					finish();
				}
				else
				{
					Toast t = Toast.makeText(v.getContext(), "Select a placement", 500);
					t.show();
				}
			}
		});
    }
}
